package com.tsingteng.cosfun.ui.found;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tsingteng.cosfun.MainActivity;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.base.BasePresenterFragment;
import com.tsingteng.cosfun.bean.ActivityListBean;
import com.tsingteng.cosfun.bean.FoundBean;
import com.tsingteng.cosfun.bean.JoinCostarBean;
import com.tsingteng.cosfun.bean.OpusBean;
import com.tsingteng.cosfun.callback.AppListener;
import com.tsingteng.cosfun.common.BroadcastManager;
import com.tsingteng.cosfun.common.Config;
import com.tsingteng.cosfun.common.Constant;
import com.tsingteng.cosfun.common.Navigate;
import com.tsingteng.cosfun.http.Url;
import com.tsingteng.cosfun.ui.found.adapter.FoundActivityAdapter;
import com.tsingteng.cosfun.utils.LogonUtils;
import com.tsingteng.cosfun.utils.NotchUtil;
import com.tsingteng.cosfun.utils.StringUtils;
import com.tsingteng.cosfun.widget.MyRecycleView;
import com.tsingteng.cosfun.widget.TitleView;

import java.util.List;
import java.util.Objects;

import butterknife.BindView;


/**
 * 发现 fragment
 * author : zht
 */

public class FoundFragment extends BasePresenterFragment<FoundPresenter, FoundContact.IFoundView> implements FoundContact.IFoundView, View.OnClickListener
        , FoundVideoPlay {


    @BindView(R.id.found_view)
    TitleView mFoundView;
    @BindView(R.id.rv_found_rv)
    MyRecycleView mRvFoundRv;
    @BindView(R.id.status_view)
    View mStatusView;
    View mReadView;
    long mId;
    int pageNo=1;
    @Override
    protected void getBundle(Bundle bundle) {
    }

    @Override
    protected void initData(View rootView) {
        mFoundView.setTitle("发现");
        if (NotchUtil.isAllScreenDevice(getActivity())){
            mStatusView.setVisibility(View.VISIBLE);
        }else{
            mStatusView.setVisibility(View.GONE);
        }
        mPresenter.getActivityList(pageNo);
        BroadcastManager.getInstance(getActivity()).addAction(Constant.JOIN, new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (null != (MainActivity)getActivity()) {
                    ((MainActivity)getActivity()).goMainFragment();
                }
            }
        });
    }

    @Override
    protected FoundPresenter createPresenter() {
        return new FoundPresenter();
    }

    @Override
    protected View initHeader() {
        return null;
    }


    @Override
    protected MyRecycleView createRecycleView() {
        return mRvFoundRv;
    }

    @Override
    protected FoundActivityAdapter createAdapter() {
        return new FoundActivityAdapter(getActivity(), this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_found;
    }

    @SuppressLint("ResourceType")
    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if (mRvFoundRv.getIsRefreshing()){
            return;
        }
        ActivityListBean activityListBean = (ActivityListBean) mAdapter.getData().get(position);
        mReadView = view.findViewById(R.id.read_view);
        mId = activityListBean.getId();
        if (StringUtils.isFastDoubleClick(500)) {
            return;
        }
        mPresenter.readActivity(mId);
    }

    @Override
    public void onLoadMoreRequested() {
        pageNo++;
        mPresenter.getActivityList(pageNo);
    }


    @Override
    protected void refresh() {
        mRvFoundRv.setmIsRefreshing(true);
        mAdapter.getData().clear();
        pageNo = 1;
        mPresenter.getActivityList(pageNo);
    }


    @Override
    public void onClick(View v) {
    }

    @Override
    public void showActivityList(FoundBean foundBean) {
        mRvFoundRv.setmIsRefreshing(false);
        mRvFoundRv.refreshComplete();
        if (mRvFoundRv != null) mRvFoundRv.refreshComplete();
        if (foundBean==null || foundBean.getActivityList()==null) return;
        if (null !=mAdapter) {
            if (1 == pageNo){
                mAdapter.getData().clear();
            }
            mAdapter.addData(foundBean.getActivityList());
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showReadActivity() {
        mPresenter.getActivityList(pageNo);
        if (StringUtils.isFastDoubleClick(500)){
            return;
        }
        Navigate.gotoActivityDetail(getActivity(), Url.ACTIVITY_URL,mId);
    }
    @Override
    public void canPlayListener(List<OpusBean> list, String position) {
        int currentPosition = Integer.parseInt(position);
        if (StringUtils.isFastDoubleClick(500)){
            return;
        }
//        Navigate.gotoVideoActivity(getActivity(), list, currentPosition);
        Navigate.gotoPlay(getActivity(),currentPosition,(int)list.get(currentPosition).getActivityId(),Constant.SORTTYPES, Config.PAGE_NO,Config.PAGE_SIZE);
    }
}
